import requests
import json
url = 'https://m.douban.com/rexxar/api/v2/subject/recent_hot/movie'

param = {
    'start': 0,
    'limit': 20
}

header = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36 Edg/138.0.0.0',
    'Referer': 'https://m.douban.com/movie/',
    'Cookie': 'll="108303"; bid=7XVADBXOfPg; __utma=30149280.1070887221.1754370640.1754370640.1754370640.1; __utmc=30149280; __utmz=30149280.1754370640.1.1.utmcsr=cn.bing.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmt=1; __utmb=30149280.1.10.1754370640; ap_v=0,6.0; _vwo_uuid_v2=D67A2FC1B597507C3F5DD9BC4E85A0B6D|2624a6824e03976b22962b6d9d4873b6',  # 替换为真实Cookie
    'Accept': 'application/json'
}
try:
    resp = requests.get(url, params=param, headers=header)
    resp.raise_for_status()  # 检查请求是否成功
    res = resp.json()
    print(res)
    file_path = r'D:\MyCode\python_project\爬虫\chapter-1\豆瓣电影.json'
    with open(file_path, mode='w', encoding='utf-8') as file:
        json.dump(res, file, ensure_ascii=False, indent=4)
except requests.RequestException as e:
    print(f'请求出错: {e}')
except json.JSONDecodeError as e:
    print(f'JSON 解析出错: {e}')
resp.close()
